//package com.cert.test.conf;
//
//import org.apache.commons.lang.StringUtils;
//import org.apache.http.HttpHost;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.DisposableBean;
//import org.springframework.beans.factory.InitializingBean;
//
//import java.io.IOException;
//import java.util.Date;
//import java.util.stream.Stream;
//
//public class EsClientDecorator implements InitializingBean, DisposableBean {
//
//    private static final Logger logger = LoggerFactory.getLogger(EsClientDecorator.class);
//
//    public static final int ADDRESS_LENGTH = 2;
//
//    private RestHighLevelClient client;
//
//    public EsClientDecorator() {
//    }
//    /**
//     * es数据查询
//     * @param searchRequest
//     * @return
//     * @throws IOException
//     */
//    public SearchResponse search(SearchRequest searchRequest)throws IOException {
//        SearchResponse response=null;
//        Date d1 = new Date();
//        String stack = "";
//        for(StackTraceElement stackTrace:Thread.currentThread().getStackTrace()){
//            stack=stack+","+stackTrace.getClassName()+"."+stackTrace.getMethodName();
//        }
//        try {
//
//            if(searchRequest==null||searchRequest.source()==null||searchRequest.source().toString().indexOf("\"from\":null,\"to\":null")>-1){
//
//                logger.debug("sendTime:"+d1.toString()+",from:"+stack+",it's illegal,dsl==>"+searchRequest.source().toString());
//                return null;
//            }
//            logger.debug("sendTime:"+d1.toString()+",from:"+stack+",dsl==>"+searchRequest.source().toString());
//            response = getRestHighLevelClient().search(searchRequest);
//        } catch (Exception e) {
//            logger.error("sendTime:"+d1.toString()+",from:"+stack+" error,exception:"+e.getMessage());
//            e.printStackTrace();
//        }
//        long took = (new Date()).getTime()-d1.getTime();
//        if(took> ElasticsearchConfig.SLOWLOG_WARN*1000){
//            logger.warn("took_millis:"+((new Date()).getTime()-d1.getTime())+",sendTime:"+d1.toString()+",from:"+stack+" over,is very slow,dsl==>"+searchRequest.source().toString());
//
//        }else if(took>ElasticsearchConfig.SLOWLOG_INFO*1000){
//            logger.info("took_millis:"+((new Date()).getTime()-d1.getTime())+",sendTime:"+d1.toString()+",from:"+stack+" over,is too slow,dsl==>"+searchRequest.source().toString());
//        }else {
//            logger.debug("took_millis:"+((new Date()).getTime()-d1.getTime())+",sendTime:"+d1.toString()+",from:"+stack+" over,dsl==>"+searchRequest.source().toString());
//
//        }
//        return response;
//    }
//    public RestHighLevelClient getRestHighLevelClient(){
//        if(client == null){
//            RestClientBuilder builder = RestClient.builder(addRestClientNode(ElasticsearchConfig.nodes));
//            //超时时间设为5分钟
////            builder.setMaxRetryTimeoutMillis(ElasticsearchConfig.maxRetryTimeoutMillis);
//            builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
//                httpAsyncClientBuilder.setMaxConnTotal(ElasticsearchConfig.maxConnTotal);
//                httpAsyncClientBuilder.setMaxConnPerRoute(ElasticsearchConfig.maxConnPerRoute);
//                return httpAsyncClientBuilder;
//            });
//            builder.setRequestConfigCallback(requestConfigBuilder ->{
//                requestConfigBuilder.setConnectTimeout(ElasticsearchConfig.connectTimeout);
//                requestConfigBuilder.setSocketTimeout(ElasticsearchConfig.socketTimeout);
//                requestConfigBuilder.setConnectionRequestTimeout(ElasticsearchConfig.connectionRequestTimeout);
//                return requestConfigBuilder;
//            });
//            //监听
//            builder.setFailureListener(new RestClient.FailureListener() {
//                public void onFailure(HttpHost host) {
//                    logger.error("RestHighLevelClient FailureListener : " + host.toString());
//                }
//            });
//            client = new RestHighLevelClient(builder);
//        }
//        return client;
//    }
//
//    @Override
//    public void destroy() throws Exception {
//        try {
//            logger.info("closing Elasticsearch");
//            if (client != null){
//                client.close();
//            }
//        }catch (Exception e){
//            logger.error("Error closing ElasticSearch client:",e);
//        }
//    }
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        client = getRestHighLevelClient();
//    }
//
//    /**
//     * 添加多个节点
//     * @param
//     * @return
//     */
//    public HttpHost[] addRestClientNode(String intAddresses){
//        String[] nodes = intAddresses.split(ElasticsearchConfig.comma);
//        if(null != nodes){
//            return Stream.of(nodes).map(this::makeHttpHost).toArray(HttpHost[]::new);
//        }
//        return null;
//    }
//
//    /**
//     * 将节点地址转换为HttpHost类型
//     * @param str 单个节点地址
//     * @return
//     */
//    private HttpHost makeHttpHost(String str) {
//        if (StringUtils.isBlank(str)) return null;
//        String[] address = str.split(ElasticsearchConfig.colon);
//        if (address.length == ADDRESS_LENGTH) {
//            String ip = address[0];
//            int port = Integer.parseInt(address[1]);
//            return new HttpHost(ip, port, ElasticsearchConfig.protocol);
//        } else {
//            return null;
//        }
//    }
//}
